package com.sunmouse.file;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;

import com.sunmouse.util.FileUtil;
import com.sunmouse.util.PrintUtil;


public class FileExe {
	public static void main(String[] args) {
		/**
		 * part one:white in a file
		 */
		String src = "D://pic/readme.txt";
		File des = FileUtil.newFile(src);
//		try {
//			BufferedOutputStream bos = new BufferedOutputStream(
//					new FileOutputStream(des));
//			bos.write("I come back again,pick me up this afternoon at the No.3 Airport,please"
//					.getBytes());
//			bos.flush();// flush
//			bos.close();
//			PrintUtil.pln("write finish");
//		} catch (FileNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		/**
		 * part two: read a file,but  not used often
		 */
		try {
			BufferedInputStream bis=new BufferedInputStream(new FileInputStream(des));
			ByteArrayOutputStream baos=new ByteArrayOutputStream();
			byte[] b=new byte[1024];
			int len=0;
			while((len=bis.read(b))!=-1){
				baos.write(b, 0, len);
				baos.flush();
			}
			PrintUtil.pln(baos.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/**
		 * part three:read a file.used often 
		 */
		try {
			BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(des)));
			String t="";
			String ds="";
			while((t=br.readLine())!=null){
				ds+=t;
			}
			PrintUtil.pln(ds.trim());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
